//
//  SetViewController.m
//  aMoment
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "SetViewController.h"
#import "AllHeaderFiles.h"

@interface SetViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *cachesPathArray;
@end

@implementation SetViewController

- (void)dealloc
{
    [_tableView release];
    [_cachesPathArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.dataSourceArray = [NSMutableArray arrayWithObjects:@"清除缓存", @"夜间模式", @"关于我们", nil];
}

- (void)loadView
{
    [super loadView];
    //获取缓存路径
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    cachesPath = [cachesPath stringByAppendingPathComponent:@"Caches"];
    self.cachesPathArray = [NSArray arrayWithObject:cachesPath];
    
    //创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
}

#pragma mark Cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

#pragma mark Cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.dataSourceArray[indexPath.row];
    
    //判断第几个cell
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", [CachesCleaner folderSizeAtPath:[_cachesPathArray firstObject]]];
    }
    if (indexPath.row == 1) {
        //判断当前是否是夜间模式
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
            cell.detailTextLabel.text = @"黑夜";
        } else {
            cell.detailTextLabel.text = @"白天";
        }
    }
    
    return cell;
}

#pragma mark Cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //找到当前点击cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //点击清除缓存
    if (indexPath.row == 0) {
        //ActionSheet
        UIAlertController *sheetController = [UIAlertController alertControllerWithTitle:nil message:@"确定清除缓存?" preferredStyle:UIAlertControllerStyleActionSheet];
        //确认按钮
        UIAlertAction *sheetAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            //AlertView确认清除提示
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"清除成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertController animated:YES completion:^{
                [alertController dismissViewControllerAnimated:YES completion:nil];
            }];
            //清除缓存
            [CachesCleaner clean];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", [CachesCleaner folderSizeAtPath:[_cachesPathArray firstObject]]];
        }];
        //取消按钮
        UIAlertAction *sheetAction2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [sheetController addAction:sheetAction1];
        [sheetController addAction:sheetAction2];
        [self presentViewController:sheetController animated:YES completion:nil];
    }
    
    //点击夜间模式
    if (indexPath.row == 1) {
        //判断当前是否是夜间模式
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
            self.view.window.alpha = 1;
            cell.detailTextLabel.text = @"白天";
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"night"];
        } else {
            self.view.window.alpha = .5;
            cell.detailTextLabel.text = @"黑夜";
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"night"];
        }
    }
    if (indexPath.row == 2) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"在此, 我们表示最诚挚的歉意, 所有的功能都还在不停地完善中, 给您带来的不便敬请谅解! 我们会在下一个版本中带来更好的体验!" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:action];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
